package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.BiEditorasDAO;
import br.urca.model.BiEditoras;

@Transactional
public class JPABiEditorasDAO implements BiEditorasDAO {
	
	private EntityManager manager;
	
	public JPABiEditorasDAO() {
		this(null);
	}
	
	@Inject
	public JPABiEditorasDAO(EntityManager manager) {
		this.manager = manager;
	}
	
	@Override
	public BiEditoras getPorId(Integer codEditora) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiEditoras b where b.codEditora = :codigo",
							BiEditoras.class).setParameter("codigo", codEditora)
					.getSingleResult();
		} catch (Exception e) {
			return null;
		}
		
	}

	@Override
	public List<BiEditoras> getPorNome(String nome) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiEditoras b WHERE b.nome LIKE '%" + nome + "%'",
							BiEditoras.class)
					.getResultList();
		} catch (Exception e) {
			return null;
		}
	}

}
